import 'dart:ui';

import 'package:get/get.dart';
import 'package:liuxlcommon/util/toast_util.dart';
import 'package:ladder_cash/Global.dart';
import 'package:ladder_cash/app/com/view/my_app_bar.dart';
import 'package:ladder_cash/app/page/component/base_page_bg.dart';
import 'package:ladder_cash/app/page/mine/setting/cancellation_page.dart';
import 'package:ladder_cash/config/super_theme.dart';
import 'package:ladder_cash/gen/assets.gen.dart';
import 'package:ladder_cash/generated/l10n.dart';
import 'package:ladder_cash/value/app_colors.dart';
import 'package:ladder_cash/value/app_const.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:liuxlcommon/view/lxl_widget.dart';
import 'package:liuxlcommon/view/my_box.dart';
import 'package:liuxlcommon/view/my_style.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:ladder_cash/app/app.dart';
import 'package:ladder_cash/app/com/view/common_view.dart';
import 'package:ladder_cash/generated/assets.dart' as myAssets;

import '../../../../library/src/logger.dart';
import '../../component/common.dart';
import '../../component/decoration_bg.dart';
import '../mine_logic.dart';

class SettingPage extends StatefulWidget {
  SettingPage({Key? key}) : super(key: key);

  @override
  _SettingPageState createState() => _SettingPageState();
}

class _SettingPageState extends State<SettingPage> {
  String packageVersion = "V1.0.0";
  final logic = Get.put(MineLogic());
  bool agree = false;
  @override
  void initState() {
    super.initState();
    PackageInfo.fromPlatform().then((info) {
      setState(() {
        var appVersion = info.version;
        packageVersion = "V$appVersion";
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return getDetail();
  }

  Widget getDetail() {
    return getBasePageBg(
        child: LColumn.eazy([
      getTabBar(
          title: S.current.setUp,
          callback: () {
            Get.back();
          },
          isShowRight: true),
      25.lsh,
      Expanded(
          child: Stack(
        children: [
          Positioned(
              top: 38.w,
              bottom: 0,
              child: Container(
                width: Global.width,
                decoration: BoxDecoration(
                    color: Colors.white, borderRadius: getTopRadius()),
                child: Column(
                  children: [
                    50.lsh,
                    LText(S.current.appName)
                        .css('font-weight: 900;font-size: 18px;'),
                    12.lsh,
                    Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        LText(S.current.version).css(
                            'font-weight: 500;font-size: 16px; color: #999999;'),
                        LText(packageVersion).css(
                            'font-weight: 500;font-size: 16px; color: #999999;')
                      ],
                    ),
                    25.lsh,
                    Container(
                      height: 116.w,
                      margin: EdgeInsets.symmetric(
                          horizontal: 12.w, vertical: 25.w),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          itemV(
                              Assets.my.settingCancellation.path,
                              S.current.accountCancellation,
                              null,
                              () => {showCancle()}),
                          itemV(Assets.my.settingLogout.path, S.current.logout,
                              null, () => {showLogout()})
                        ],
                      ),
                    )
                  ],
                ),
              )),
          Positioned(
            top: 0,
            width: Global.width,
            child: Center(
                child: Image.asset(
              Assets.img.logo.path,
              width: 76.w,
              height: 76.w,
            )),
          ),
        ],
      ))
    ]).size(375.w, null));
  }

  itemV(icon, name, value, onTap) {
    return InkWell(
      onTap: () {
        onTap();
      },
      child: Container(
          width: 164.w,
          padding: const EdgeInsets.all(12),
          decoration: const BoxDecoration(
              color: SuperTheme.infoBg,
              borderRadius: BorderRadius.all(Radius.circular(8))),
          child: Column(
              crossAxisAlignment: CrossAxisAlignment.center,
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                if (icon != null) LImage.easy(icon, 24.w, 24.w),
                20.lsh,
                LText.easy(name, 14.sp, fw5).flex(1),
                10.lsh,
                if (value != null) LText.easy(value, 15.sp, fw5),
              ])),
    );
  }

  showCancle() {
    Get.dialog(Column(
      children: [
        Expanded(
          child: Container(),
        ),
        SizedBox(
          height: 628.w,
          child: Column(
            children: [
              Container(
                padding: EdgeInsets.symmetric(horizontal: 20.w),
                height: 30.w,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.end,
                  children: [
                    InkWell(
                      onTap: () {
                        Get.back();
                      },
                      child: Image.asset(
                        Assets.cer.cerClose.path,
                        width: 30.w,
                        height: 30.w,
                      ),
                    ),
                  ],
                ),
              ),
              30.lsh,
              Container(
                margin: EdgeInsets.symmetric(horizontal: 20.w),
                padding: EdgeInsets.only(
                    left: 20, right: 20.w, bottom: 10.w, top: 40.w),
                height: 568.w,
                decoration: BoxDecoration(
                    image: getDecorationBg(Assets.cer.cerPanBg.path)),
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    textBorder(S.current.pleaseConfirm,
                        fontWeight: FontWeight.w900,
                        fontsize: 24.sp,
                        color: SuperTheme.textTitltPrimary,
                        borderColor: Colors.black,
                        fontFamily: 'italic'),
                    Image.asset(
                      Assets.my.cancle.path,
                      width: 157,
                      height: 145.w,
                    ),
                    middleFont(S.current.itCannot, fontsize: 16.sp),
                    xyBox(context),
                    Container(
                        height: 46.w,
                        child: commonBtn(S.current.confirm, callback: () {
                          if (agree) {
                            App.logout();
                          } else {
                            ToastUtils.toast(App.localeEn
                                ? "Please agree to the agreement"
                                : "Por favor acepta el acuerdo");
                          }
                        })),
                  ],
                ),
              ),
            ],
          ),
        ),
        Expanded(child: Container())
      ],
    ));
  }

  Widget xyBox(BuildContext context) {
    return Builder(
      builder: (context) {
        return StatefulBuilder(builder: (context, setState) {
          return LRow(
            lStyle: LStyle(
                width: double.infinity,
                crossAxisAlignment: CrossAxisAlignment.center,
                mainAxisAlignment: MainAxisAlignment.center),
            children: [
              LImage(
                agree ? Assets.login.checked.path : Assets.login.check.path,
                lStyle: LStyle(
                    padding: EdgeInsets.all(6.w),
                    height: 30.w,
                    margin: EdgeInsets.symmetric(horizontal: 0.w)),
              ),
              Expanded(
                child: LText.easy(S.current.accountCancellationT3, 12.sp,
                    FontWeight.w400, SuperTheme.grey9F),
              )
            ],
            onTap: () {
              setState(() {
                agree = !agree;
              });
            },
          );
        });
      },
    );
  }

  showLogout() {
    Get.dialog(Column(
      children: [
        Expanded(
          child: Container(),
        ),
        SizedBox(
          height: 440.w,
          child: Column(
            children: [
              Container(
                padding: EdgeInsets.symmetric(horizontal: 20.w),
                height: 30.w,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.end,
                  children: [
                    InkWell(
                      onTap: () {
                        Get.back();
                      },
                      child: Image.asset(
                        Assets.cer.cerClose.path,
                        width: 30.w,
                        height: 30.w,
                      ),
                    ),
                  ],
                ),
              ),
              30.lsh,
              Container(
                margin: EdgeInsets.symmetric(horizontal: 20.w),
                padding: EdgeInsets.only(
                    left: 20, right: 20.w, bottom: 10.w, top: 40.w),
                height: 335.w,
                decoration: BoxDecoration(
                    image: getDecorationBg(Assets.cer.cerPanBg.path)),
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    textBorder(S.current.logout,
                        fontWeight: FontWeight.w900,
                        fontsize: 24.sp,
                        color: SuperTheme.textTitltPrimary,
                        borderColor: Colors.black,
                        fontFamily: 'italic'),
                    Image.asset(
                      Assets.my.outPeople.path,
                      width: 189,
                      height: 126.w,
                    ),
                    textBorder(S.current.areYou),
                    Container(
                        height: 46.w,
                        child: commonBtn(S.current.confirm, callback: () {
                          App.logout();
                        })),
                  ],
                ),
              ),
            ],
          ),
        ),
        Expanded(child: Container())
      ],
    ));
  }

  get itemBox =>
      LStyle(height: 56.w, padding: EdgeInsets.symmetric(horizontal: 16.w));
}
